﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Paint.DefinePaintBrush.CUiBrushWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.Paint.DefinePaintBrush;

public class CUiBrushWindow : TxWindow, IComponentConnector
{
  private CApBrushViewModel _viewModel;
  private TxWindow _parent;
  private CApSolidValidator _solidValidator;
  private CApOriginFrameValidator _originFrameValidator;
  private CApPaintBrush _storeSelectedBrush;
  private bool isSucceed;
  private string _title;
  internal TextBlock _label;
  internal System.Windows.Controls.TextBox _brushName;
  internal TextBlock _labelSolid;
  internal TxObjEditBoxControl _solidFiled;
  internal TextBlock _labelOriginFrame;
  internal TxObjEditBoxControl _originFrameFiled;
  internal System.Windows.Controls.Button _okBtn;
  internal System.Windows.Controls.Button _cancelBtn;
  private bool _contentLoaded;

  public CUiBrushWindow(CApBrushViewModel viewModel, TxWindow parent)
  {
    this.InitializeComponent();
    this._viewModel = viewModel;
    this.isSucceed = false;
    this._storeSelectedBrush = (CApPaintBrush) ((CUiDefineBrushesWindow) parent)._brushesGrid.SelectedItem;
    this._viewModel.InitEditedBrush();
    this._parent = parent;
    ((Window) this._parent).Closed += new EventHandler(this.Parent_Closed);
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    ((FrameworkElement) this).MinHeight = ((FrameworkElement) this).ActualHeight;
    ((FrameworkElement) this).MaxHeight = ((FrameworkElement) this).ActualHeight;
    this._solidValidator = new CApSolidValidator();
    this._originFrameValidator = new CApOriginFrameValidator();
    this._solidFiled.SetValidator((ITxValidator) this._solidValidator);
    this._originFrameFiled.SetValidator((ITxValidator) this._originFrameValidator);
    ((UIElement) this._parent).IsEnabled = false;
    if (!this.IsInDesignMode)
    {
      this._brushName.Focus();
      ((FrameworkElement) this).DataContext = (object) this._viewModel;
      this.RegisterForEvents();
    }
    if (this._viewModel.EditedBrush.BrushId == null)
    {
      this._title = $"{DnProcessSimulateCommands.Paint.DefinePaintBrush.Resources.StringTable.ADD_BRUSH_DIALOG_TITLE} - {((TxBaseGeometryCreationObject) this._viewModel.EditedBrush.Robot).Name}";
    }
    else
    {
      this._solidValidator.IsEditMode = true;
      this._originFrameValidator.IsEditMode = true;
      this._title = $"{DnProcessSimulateCommands.Paint.DefinePaintBrush.Resources.StringTable.EDIT_BRUSH_DIALOG_TITLE} - {((TxBaseGeometryCreationObject) this._viewModel.EditedBrush.Robot).Name}";
    }
    // ISSUE: explicit non-virtual call
    __nonvirtual (((Window) this).Title) = this._title;
  }

  private void TxWindow_Closed(object sender, EventArgs e)
  {
    if (this._viewModel.EditedBrush.BrushId == null)
    {
      this._viewModel.CurrentBrush = this._storeSelectedBrush;
      this._viewModel.EditedBrush = this._storeSelectedBrush;
    }
    if (!this.isSucceed)
      this._viewModel.ShowGhostBrush();
    ((UIElement) this._parent).IsEnabled = true;
    this.UnregisterForEvents();
  }

  private void OkBtn_Click(object sender, RoutedEventArgs e)
  {
    if (!this._viewModel.IsBrushNameExists(this._brushName.Text))
    {
      if (!this._viewModel.UpdateEditedBrush())
      {
        int num = (int) TxMessageBox.Show(DnProcessSimulateCommands.Paint.DefinePaintBrush.Resources.StringTable.BRUSH_ERROR, this._title, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
      this.isSucceed = true;
      ((Window) this).Close();
    }
    else
    {
      int num = (int) TxMessageBox.Show(string.Format(DnProcessSimulateCommands.Paint.DefinePaintBrush.Resources.StringTable.BRUSH_NAME_EXISTS, (object) this._viewModel.EditedBrush.Name, (object) ((TxBaseGeometryCreationObject) this._viewModel.EditedBrush.Robot).Name), this._title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
      this._brushName.Focus();
    }
  }

  private void CancelBtn_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void BrushName_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  {
    this._viewModel.EditedBrush.Name = this._brushName.Text;
  }

  private void Parent_Closed(object sender, EventArgs e)
  {
    ((Window) this._parent).Closed -= new EventHandler(this.Parent_Closed);
    ((Window) this).Close();
  }

  private void SolidPicker_TypeInvalid(object sender, EventArgs e)
  {
    if (!TxFrame.op_Equality(this._viewModel.EditedBrush.OriginFrame, (ITxObject) null))
      return;
    if (this._viewModel.EditedBrush.Solid == null)
      this._solidValidator.ParentComponent = (ITxComponent) null;
    this._originFrameValidator.ParentComponent = (ITxComponent) null;
  }

  private void SolidPicker_TypeValid(object sender, EventArgs e)
  {
    if (this._solidValidator.ParentComponent == null)
      return;
    this._originFrameFiled.Focus();
  }

  private void SolidPicker_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    this._originFrameValidator.ParentComponent = this._solidValidator.ParentComponent;
    if (this._solidValidator.ParentComponent == null)
      return;
    this._originFrameFiled.Focus();
  }

  private void Origin_Frame_TypeInvalid(object sender, EventArgs e)
  {
    if (this._viewModel.EditedBrush.Solid != null)
      return;
    if (TxFrame.op_Equality(this._viewModel.EditedBrush.OriginFrame, (ITxObject) null))
      this._originFrameValidator.ParentComponent = (ITxComponent) null;
    this._solidValidator.ParentComponent = (ITxComponent) null;
  }

  private void Origin_Frame_TypeValid(object sender, EventArgs e)
  {
  }

  private void Origin_Frame_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    this._solidValidator.ParentComponent = this._originFrameValidator.ParentComponent;
  }

  private void RegisterForEvents()
  {
    ((Window) this).Closing += new CancelEventHandler(this._viewModel.OnWindowClosing);
  }

  private void UnregisterForEvents()
  {
    ((Window) this).Closing -= new CancelEventHandler(this._viewModel.OnWindowClosing);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/paint/definepaintbrush/cuibrushwindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        ((Window) target).Closed += new EventHandler(this.TxWindow_Closed);
        break;
      case 2:
        this._label = (TextBlock) target;
        break;
      case 3:
        this._brushName = (System.Windows.Controls.TextBox) target;
        this._brushName.KeyUp += new System.Windows.Input.KeyEventHandler(this.BrushName_KeyUp);
        break;
      case 4:
        this._labelSolid = (TextBlock) target;
        break;
      case 5:
        this._solidFiled = (TxObjEditBoxControl) target;
        this._solidFiled.TypeInvalid += new EventHandler(this.SolidPicker_TypeInvalid);
        this._solidFiled.TypeValid += new EventHandler(this.SolidPicker_TypeValid);
        this._solidFiled.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.SolidPicker_Picked);
        break;
      case 6:
        this._labelOriginFrame = (TextBlock) target;
        break;
      case 7:
        this._originFrameFiled = (TxObjEditBoxControl) target;
        this._originFrameFiled.TypeInvalid += new EventHandler(this.Origin_Frame_TypeInvalid);
        this._originFrameFiled.TypeValid += new EventHandler(this.Origin_Frame_TypeValid);
        this._originFrameFiled.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.Origin_Frame_Picked);
        break;
      case 8:
        this._okBtn = (System.Windows.Controls.Button) target;
        this._okBtn.Click += new RoutedEventHandler(this.OkBtn_Click);
        break;
      case 9:
        this._cancelBtn = (System.Windows.Controls.Button) target;
        this._cancelBtn.Click += new RoutedEventHandler(this.CancelBtn_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
